<?php

namespace App\Http\Requests;

use App\Models\ProductSku;
use Illuminate\Foundation\Http\FormRequest;

class AddCartRequest extends Request
{
    public function rules()
    {
        return [
            'sku_id' => [
                'required',
                function($attribute, $value, $fail){
                    if(! $sku = ProductSku::find($value)){
                        return $fail('该商品不存在');
                    }
                    if(! $sku->product->on_sale){
                        return $fail('该商品未上架');
                    }
                    if($sku->stock === 0){
                        return $fail('该商品已售完');
                    }
                    if($this->input('amount') > 0 && $sku->stock < $this->input('amount')){
                        return $fail('该商品库存不足');
                    }
                }
            ],
            'amount' => ['required','integer','min:1']
        ];
    }

    public function attributes()
    {
        return [
            'amount' => '商品数量'
        ];
    }

    public function messages()
    {
        return [
            'sku_id.required' => '请选择商品'
        ];
    }
}
